<?php
class Cms_Model_Course
{
	private static $_instance;
	private static $_cacheTimeout = 300;
	
	private function __construct() {
	}
	
	/**
	 * @return Cms_Model_Course
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * Get list of Courses show
	 * @param array $options 
	 * @return array
	 */
	public function getCourses($options=array()) {
        $result = array();
	    $courseDao = Cms_Model_DAO_Course::factory();
		try {
			$result = $courseDao->getCourses($options);;
		} catch (Exception $exc) {
		    prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__ );
		}
		return $result;
	}
	
	public function getCoursesWithSub($options=array()) {
	    $result = array();
	    $courseDao = Cms_Model_DAO_Course::factory();
	    try {
	    	$rawResult = $courseDao->getAllCourses($options);
	    	foreach ($rawResult as $raw) {
	    	    if(!$raw['parent']) {//Khoa hoc cha
	    	        if(!$result[$raw['id']]['id']) {
    	    	        $result[$raw['id']] = $raw;
    	    	        $result[$raw['id']]['childs'] = array();	    	        
	    	        } else {
	    	            foreach ($raw as $key => $val) {
	    	                $result[$raw['id']][$key] = $val;
	    	            }
	    	        }
	    	    } else {//Khoa hoc con
	    	        if(!$result[$raw['parent']]['id']) {
	    	            $result[$raw['parent']]['id']= $raw['parent'];
	    	            $result[$raw['parent']]['childs'][] = $raw; 
	    	        } else {
	    	            $result[$raw['parent']]['childs'][] = $raw;
	    	        }
	    	    }
// 	    	    if(!$result[$raw['id']]) {
// 	    	    	$tempArr = array('id' => $raw['id'], 'image' => $raw['image'], 'hash_folder' => $raw['hash_folder'],
// 	    	    	        'location' => $raw['location'], 'last_update' => $raw['last_update'], 'lang' => $raw['lang'],
// 	    	    	        'title_seo' => $raw['seo_title'],
// 	    	    			'title' => $raw['content'],
// 	    	    			'childs' => array(),);
// 	    	    	if($raw['child_id']) {
// 	    	    		$tempArr['childs'][] = array('id' => $raw['child_id'], 'title_seo' => $raw['seo_title'],'title' => $raw['content']);
// 	    	    	}
// 	    	    	$result[$raw['id']] = $tempArr;
// 	    	    } else {
// 	    	    	$result[$raw['id']]['childs'][] = array('id' => $raw['child_id'], 'title_seo' => $raw['seo_title'], 'title' => $raw['content']);
// 	    	    }
	    	}
	    } catch (Exception $exc) {
	    	prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__ );
	    }
	    return $result;
	}
	
	public function getTabs($courseId, $lang) {
	    $result = array();
	    if(!$courseId || !$lang) {
	        return $result;
	    }
	    
	    $dao = Cms_Model_DAO_Course::factory();
	    try {
	        $result = $dao->getTabs(array('id' => $courseId, 'lang' => $lang));
	    }catch (Exception $exc) {
	        prBinh($exc);
	        Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
	
	/**
	 * Get course detail by given id
	 * @param integer $id
	 * @return array
	 * */
	public function getCourseById($id) {
		$course = array();
		if(!$id) {
			return $course;
		}
		
		//Get from cache first
		$cache = Utils_Global::getCacheInstance('cms');
		$keyConfig = Utils_Global::getConfig('cache', 'keys', 'keys');
		$key = $keyConfig->course_select . '_id_' . $id;
		if(is_object($cache)) {
			$course = $cache->getCache($key);
			if($course) {
				return $course;
			}
		}
		
		$courseDao = Cms_Model_DAO_Course::factory();
		$options = array('id' => $id);
		try {
			$course = $courseDao->getCourses($options);
			if($course && is_object($cache)) {
				$cache->setCache($key, $course);
			}
		} catch (Exception $exc) {
		    prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		
		return $course;
	}
	
	/**
	 * Get course count by given condition
	 * @package array $options
	 * @return integer
	 * */
	public function getCourseCount($options=array()) {
		//Get from cache first
		$cache = Utils_Global::getCacheInstance('cms');
		$keyConfig = Utils_Global::getConfig('cache', 'keys', 'keys');
		$key = $keyConfig->course_count;
		if(is_object($cache)) {
			$result = $cache->getCache($key);
			if($result) {
				return $result;
			}
		}
		
		$courseDao = Cms_Model_DAO_Course::factory();
		try {
			$result = $courseDao->getCourseCount($options);
			if($result && is_object($cache)) {
				$cache->setCache($key, $result);
			}
		} catch (Exception $exc) {
		    prBinh($exc);
			Utils_Global::storeLog($exc, __FILE__, __LINE__);
		}
		
		return $result;
	}
	
	/**
	 * Get course by given keyword
	 * @param string $keySearch
	 * @param integer $offset
	 * @param integer $limit
	 * @param array $options
	 * @return array
	 * */
	public function searchFullText($keySearch, $offset=0, $limit=10, $options=null) {
	    $result = array();
	    try {
	        $courseDao = Cms_Model_DAO_Course::factory();
	        $result = $courseDao->searchFullText($keySearch, $offset, $limit, $options);
	    } catch (Exception $exc) {
	        prBinh($exc);
	        Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
	
	/**
	 * Get count by keyword
	 * @param string $keySearch
	 * @return integer
	 * */
	public function getCountFullText($keySearch) {
	    $count = 0;
	    try {
	    	$courseDao = Cms_Model_DAO_Course::factory();
	    	$result = $courseDao->countFullText($keySearch);
	    } catch (Exception $exc) {
	    	prBinh($exc);
	    	Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	     
	    return $result;
	}
}